/*
 * 
 * Version 1.00
 * 
 * Created on 2007-6-4
 * Encoding GB2312
 */
package net.comm.group.service.impl;

import java.sql.Connection;
import java.util.List;
import java.util.Vector;

import net.comm.common.ReturnMessage;
import net.comm.common.common;

import net.comm.group.bean.BaseGdpTotal;
import net.comm.group.db.DbGdpTotalService;
import net.comm.group.db.GdpTotalServiceSqlMysql;
import net.comm.group.web.form.GdpTotalForm;
import net.comm.page.PageCtr;

import net.comm.util.DatetimeUtil;
import net.comm.util.DbcpPool;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;
import org.sakaiproject.db.impl.BasicSqlService;


/**
 *
 * @author hcgong(hcgong@sina.com)
 * @version 1.0
 * @date 2007-6-4
 */
public class GdpTotalServiceImpl {

	static Logger logger = Logger.getLogger(GdpTotalServiceImpl.class.getName());

	/**
	 * 
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#list(net.swift.module.other.ExaQueryBean, net.swift.common.ReturnMessage)
	 */
	public List list(GdpTotalForm theForm,  ReturnMessage msg) 
	{
		List list = new Vector();

		Connection cn = null;

		try
		{
			cn = DbcpPool.getConnection();
			
			GdpTotalServiceSqlMysql servSql = new GdpTotalServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields2 = new Object[2];
			
			fields2[0] = theForm.getQryGroupID();
			fields2[1] = theForm.getQryDsort();
			
			/*
			 * 获得查询SQL语句
			 */
			String sql = servSql.getCountSql3();

			/**
			 * 根据SQL执行结果计算分页相关信息
			 */
			PageCtr page = theForm.getPage();
			page.setTotalNum(bSqlServ.dbCount(cn, sql, fields2));
			page.caculate();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[4];
			
			fields[0] = theForm.getQryGroupID();
			fields[1] = theForm.getQryDsort();
			fields[2] = page.getPos()-1;
			fields[3] = page.getPageSize();
			
			sql = servSql.getRecbyPageSql();
			
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdpTotalService());
			
		    DbcpPool.returnConnection(cn);
		    
		} catch(Exception ex) 
		{
			/**
			 * 记录相关日志
			 */
			logger.error(ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
			
		} finally {
			DbcpPool.returnConnection(cn);
		}		
		
		return list;
	}

	/**
	 * @see net.IUserbasicService.module.other.IUserService
	 * 		#get(net.swift.db.tab_paramnostro, net.swift.common.ReturnMessage)
	 */
	public void get(GdpTotalForm theForm, ReturnMessage msg) 
	{
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			List list = null;

			GdpTotalServiceSqlMysql servSql = new GdpTotalServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getUserID();			
			fields[1] = theForm.getGroupID();			
			fields[2] = theForm.getDsort();			
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields, new DbGdpTotalService());
			
			if(list.size() ==0){
				common.setAppFailMsg(msg , " 取记录失败，请检查！");
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;				
			}
			BaseGdpTotal base = (BaseGdpTotal)list.get(0);
				
			BeanUtils.copyProperties(theForm, base);
			
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 添加回复
	 * @see net.IUserbasicService.module.other.IUserService#add(net.swift.common.ReturnMessage)
	 */
	public void add(GdpTotalForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			
			DbcpPool.beginTran(cn);

			GdpTotalServiceSqlMysql servSql = new GdpTotalServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields = new Object[7];
			
			fields[0] = theForm.getUserID();			
			fields[1] = theForm.getGroupID();
			fields[2] = theForm.getDsort();
			fields[3] = theForm.getUpNum();
			fields[4] = theForm.getDownNum();
			fields[5] = theForm.getTotal();
			fields[6] = DatetimeUtil.getDatetime();
			
			String sql = servSql.getInsertSql();
						
			boolean res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 登记失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);

		} catch(Exception ex) 
		{
			common.setAppFailMsg(msg , ex.getMessage());
			logger.error("System error , param get about operlog" + ex);
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}

	/**
	 * 删除一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void delete(GdpTotalForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);

			GdpTotalServiceSqlMysql servSql = new GdpTotalServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();

			//取得FORM中传入的查询参数
			Object[] fields = new Object[3];
			
			fields[0] = theForm.getUserID();			
			fields[1] = theForm.getGroupID();			
			fields[2] = theForm.getDsort();			
			
			
			String sql = servSql.getDeleteSql();
								
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新记录失败，请检查！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(GdpTotalForm theForm, ReturnMessage msg) 
	{
		
		Connection cn = null;
		try
		{
			cn = DbcpPool.getConnection();
			DbcpPool.beginTran(cn);
			
			update(cn, theForm, msg);
			if(msg.getErrCode() != 0){
				return;
			}
			
			DbcpPool.commitTran(cn);
			DbcpPool.closeTran(cn);
			DbcpPool.returnConnection(cn);
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
			DbcpPool.returnConnection(cn);
		}
		
		return ;
	}
	/**
	 * 更新一条指定记录
	 * @see net.IUserbasicService.module.other.IUserService#update(net.swift.common.ReturnMessage)
	 */
	public void update(Connection cn, GdpTotalForm theForm, ReturnMessage msg) 
	{
		
		try
		{
			
			List list = null;

			GdpTotalServiceSqlMysql servSql = new GdpTotalServiceSqlMysql();
			BasicSqlService  bSqlServ = new BasicSqlService();
			
			//取得FORM中传入的查询参数
			Object[] fields3 = new Object[3];
			
			fields3[0] = theForm.getUserID();			
			fields3[1] = theForm.getGroupID();			
			fields3[2] = theForm.getDsort();			
			
			String sql = servSql.getbykeySql();			
					
			list = bSqlServ.dbRead(cn, sql, fields3, new DbGdpTotalService());
			if(list.size() ==0){
				//增加新记录
				Object[] fields = new Object[7];
				
				fields[0] = theForm.getUserID();			
				fields[1] = theForm.getGroupID();
				fields[2] = theForm.getDsort();
				fields[3] = theForm.getUpNum();
				fields[4] = theForm.getDownNum();
				fields[5] = theForm.getTotal();
				fields[6] = DatetimeUtil.getDatetime();
				
				sql = servSql.getInsertSql();
							
				boolean res = bSqlServ.dbWrite(cn, sql, fields);
				if(!res){
					common.setAppFailMsg(msg , " 登记失败！");
					DbcpPool.rollBackTran(cn);
					DbcpPool.closeTran(cn);
					DbcpPool.returnConnection(cn);
					return ;
				}
				return;				
			}
			BaseGdpTotal base = (BaseGdpTotal)list.get(0);
				
			//更新
			Object[] fields = new Object[7];
			
			fields[0] = base.getUpNum() + theForm.getUpNum();
			fields[1] = base.getDownNum() + theForm.getDownNum();
			fields[2] = base.getUpNum() + theForm.getUpNum() + theForm.getDownNum();
			fields[3] = base.getMtime();
			fields[4] = theForm.getUserID();			
			fields[5] = theForm.getGroupID();
			fields[6] = theForm.getDsort();
			
			sql = servSql.getUpdateSql();
						
			boolean  res = bSqlServ.dbWrite(cn, sql, fields);
			if(!res){
				common.setAppFailMsg(msg , " 更新失败！");
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
				return ;
			}			
			
			return;
			
		} catch(Exception ex) 
		{
			logger.error("System error , param get about invite" + ex);
			common.setAppFailMsg(msg , ex.getMessage());
			try 
			{
				DbcpPool.rollBackTran(cn);
				DbcpPool.closeTran(cn);
				DbcpPool.returnConnection(cn);
			} catch(Exception ex1) 
			{
				ex1.printStackTrace();
				logger.error("事务回滚操作失败!ExaServiceImpl->add(01)" , ex1);
			}			
		} finally {
		}
		
		return ;
	}
}
